package com.ai.bitcoin.weixin.push;

import com.ai.bitcoin.bo.PushBO;
import com.ai.bitcoin.constant.CommonConstant;
import com.ai.bitcoin.init.ThreadPoolService;
import com.ai.bitcoin.jpa.dao.BccSubCoinDao;
import com.ai.bitcoin.jpa.entity.BccBasicInfo;
import com.ai.bitcoin.jpa.entity.BccSubCoin;
import com.ai.bitcoin.jpa.service.BccBasicInfoService;
import com.ai.bitcoin.redis.service.BccRedisService;
import com.ai.bitcoin.service.impl.HtmlReportServiceImpl;
import com.ai.bitcoin.util.DateUtil;
import com.ai.bitcoin.vo.HtmlReportGenerateVO;
import com.ai.bitcoin.weixin.template.CoinReportTemplate;
import com.ai.bitcoin.weixin.template.SendReportService;
import com.ai.bitcoin.weixin.template.TemplateManager;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;
import java.util.concurrent.ExecutorService;

/**
 * @description: 币的定时推送
 * @author: gulj
 * @create: 2018-06-13 00:29
 **/
@Component
@Slf4j
public class WxPushHelper {

    @Autowired
    private ThreadPoolService threadPoolService;

    @Autowired
    private SendReportService sendReportService;

    @Autowired
    private HtmlReportServiceImpl htmlReportService;

    @Autowired
    private BccRedisService bccRedisService;

    @Autowired
    private BccSubCoinDao bccSubCoinDao;

    @Autowired
    private BccBasicInfoService bccBasicInfoService;

    private static String PUSH_REPORT_PREFIX_URL = "https://www.valuechain.group/template/index.html?";

    public void pushReport(PushBO pushBO) {
        List<BccSubCoin> subCoins;

        if (pushBO == null) {
            subCoins = bccSubCoinDao.queryValidRecord();
        }
        else {
            subCoins = Collections.singletonList(new BccSubCoin(pushBO.getBccId(), pushBO.getOpenId(), pushBO.getBccName()));
        }


        if (subCoins == null || subCoins.size() <= 0) {
            log.error("今天无有效币可推送，{}", new Date());
            return;
        }

        // 推送微信模板
        ExecutorService wxTemplatePool = threadPoolService.getWxTemplatePool();
        for (BccSubCoin subCoin : subCoins) {
            String openId = subCoin.getUserOpenId();
            String bccName = subCoin.getBccName();
            String bccId = subCoin.getBccId();

            //订阅币英文名为空问题
            if (StringUtils.isEmpty(bccName)) {
                BccBasicInfo basicInfo = bccBasicInfoService.findByBccId(bccId);
                bccName = basicInfo.getNameEn();
                subCoin.setBccName(bccName);
                bccSubCoinDao.save(subCoin);
            }

            Map<String, Object> templeMap = new HashMap<String, Object>();
            templeMap.put("openId", openId);
            templeMap.put("url", PUSH_REPORT_PREFIX_URL + "bccId=" + bccId + "T" + DateUtil.currentDate2Str(CommonConstant.REPORT_HTML_PREFIX_PATH));
            templeMap.put("first", "您有一个新币(" + bccName + ")报告已生成，点击可查看报告内容。");
            templeMap.put("keyword1", bccName);
            templeMap.put("keyword2", bccName);
            templeMap.put("remark", "感谢您使用评链网推送服务，如果不想接收到此推送，可点击下方公众号菜单[我的-个人中心]，进行取消推送或者订阅");

            wxTemplatePool.submit(() -> {
                //慢慢发 不用考虑速度
                sendReportService.sendTemplateMsg(TemplateManager.buildTemplateParater(new CoinReportTemplate(), templeMap));
            });
        }
    }


    public void batchGenerateReport(List<String> bccIdList) {
        if (bccIdList != null && bccIdList.size() > 0) {
            for (String aBccIdList : bccIdList) {
                try {
                    HtmlReportGenerateVO completeReport = htmlReportService.createCompleteReport(aBccIdList);
                    log.info("【币的报告内容】 币：{},内容：{}", aBccIdList, completeReport);
                    bccRedisService.cache2BitCoinReport(aBccIdList, completeReport);

                } catch (Exception e) {
                    log.error("【定时任务，生成所有币】异常栈信息：{}", e.getMessage());
                }
            }
        }
    }
}